package com.example.springboot.service.impl;

import com.example.springboot.mapper.LeaveRequestMapper;
import com.example.springboot.mapper.WorkerMapper;
import com.example.springboot.pojo.LeaveRequest;
import com.example.springboot.pojo.PageBean;
import com.example.springboot.pojo.User;
import com.example.springboot.pojo.Worker;
import com.example.springboot.service.LeaveRequestService;
import com.example.springboot.service.WorkerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeaveRequestServiceImpl implements LeaveRequestService {

    @Autowired
    private LeaveRequestMapper leaveRequestMapper;
    @Autowired
    private WorkerMapper workerMapper;

    @Override
    public PageBean<LeaveRequest> getList(Integer pageNum,Integer pageSize) {
        PageBean<LeaveRequest> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<LeaveRequest> lrs = leaveRequestMapper.getList();
        Page<LeaveRequest> p = (Page<LeaveRequest>) lrs;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void addRequest(String workerId, String detail, String start, String end) {
        Worker w = workerMapper.findByWorkerId(workerId);
        leaveRequestMapper.addRequest(workerId,w.getName(),w.getDepartment(),w.getPhoneNumber(),detail,start,end);
    }

    @Override
    public void pass(Integer id) {
        leaveRequestMapper.pass(id);
    }

    @Override
    public void refuse(Integer id) {
        leaveRequestMapper.refuse(id);
    }

    @Override
    public PageBean<LeaveRequest> getByWorkerId(Integer pageNum, Integer pageSize, String workerId) {
        PageBean<LeaveRequest> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<LeaveRequest> lrs = leaveRequestMapper.getByWorkerId(workerId);
        Page<LeaveRequest> p = (Page<LeaveRequest>) lrs;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
}
